package com.yanq.liancar.activity;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/17  10:27
 *  @描述       ${TODO}
 */


import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.JiyouAdapter;
import com.yanq.liancar.adapter.MaintainAdapter1;
import com.yanq.liancar.adapter.MaintainAdapter2;
import com.yanq.liancar.adapter.PeijianAdapter;
import com.yanq.liancar.bean.FindOdbBindBean;
import com.yanq.liancar.bean.QueryObdKeepHisListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MaintainActiviity extends BaseToolbarActivity {
    @BindView(R.id.tv_EngineOil)
    TextView tvEngineOil;
    @BindView(R.id.view_EngineOil)
    View viewEngineOil;
    @BindView(R.id.ll_EngineOil)
    LinearLayout llEngineOil;
    @BindView(R.id.tv_parts)
    TextView tvParts;
    @BindView(R.id.view_parts)
    View viewParts;
    @BindView(R.id.ll_parts)
    LinearLayout llParts;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_right2)
    ImageView ivRight2;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.common_toolbar)
    Toolbar commonToolbar;
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.tv_jiance)
    TextView tvJiance;
    @BindView(R.id.tv_baoyang)
    TextView tvBaoyang;
    @BindView(R.id.recyclerview_jiyou)
    RecyclerView recyclerviewJiyou;
    @BindView(R.id.recyclerview_peijian)
    RecyclerView recyclerviewPeijian;

    private MaintainAdapter1 mMaintainAdapter1;
    private MaintainAdapter2 mMaintainAdapter2;
    private List<QueryObdKeepHisListBean.DataBean._$1Bean> mList1 = new ArrayList<>();
    private List<QueryObdKeepHisListBean.DataBean._$2Bean> mList2 = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        mTvTitle.setText("养护提醒");
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setTextColor(getResources().getColor(R.color.main_color));
        mTvRight.setText("设置");

        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxActivityTool.skipActivity(MaintainActiviity.this, MaintainSetActivity.class);
            }
        });

        initOkGo();
        initView();
        //
        //        progesssJiyou.setProgress(6);
        //
        //        setPosWay();

    }

    private void initView() {
        tvEngineOil.setTextColor(getResources().getColor(R.color.main_color));
        viewEngineOil.setVisibility(View.VISIBLE);
        recyclerviewJiyou.setVisibility(View.VISIBLE);

        tvParts.setTextColor(getResources().getColor(R.color.background_content));
        viewParts.setVisibility(View.GONE);
        recyclerviewPeijian.setVisibility(View.GONE);

        recyclerviewJiyou.setNestedScrollingEnabled(false);
        recyclerviewPeijian.setNestedScrollingEnabled(false);
        recyclerviewJiyou.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerviewPeijian.setLayoutManager(new LinearLayoutManager(mContext));
        mMaintainAdapter1 = new MaintainAdapter1(R.layout.item_maintain, mList1);
        mMaintainAdapter2 = new MaintainAdapter2(R.layout.item_maintain, mList2);
        recyclerviewJiyou.setAdapter(mMaintainAdapter1);
        recyclerviewPeijian.setAdapter(mMaintainAdapter2);
    }

    //    @Override
    //    public void onWindowFocusChanged(boolean hasFocus) {
    //        super.onWindowFocusChanged(hasFocus);
    //        if (hasFocus) {
    //            setPos(rlJiyo,progesssValueJiyou,progesssJiyou);
    //        }
    //    }

    //    private void setPosWay() {
    //        tv_jiyou.post(new Runnable() {
    //            @Override
    //            public void run() {
    //                setPos(rlJiyo, progesssValueJiyou, progesssJiyou);
    //            }
    //        });
    //    }


    @OnClick({R.id.ll_EngineOil, R.id.ll_parts, R.id.tv_jiance, R.id.tv_baoyang})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_EngineOil:
                tvEngineOil.setTextColor(getResources().getColor(R.color.main_color));
                viewEngineOil.setVisibility(View.VISIBLE);
                recyclerviewJiyou.setVisibility(View.VISIBLE);

                tvParts.setTextColor(getResources().getColor(R.color.background_content));
                viewParts.setVisibility(View.GONE);
                recyclerviewPeijian.setVisibility(View.GONE);
                break;
            case R.id.ll_parts:
                tvEngineOil.setTextColor(getResources().getColor(R.color.background_content));
                viewEngineOil.setVisibility(View.GONE);
                recyclerviewJiyou.setVisibility(View.GONE);

                tvParts.setTextColor(getResources().getColor(R.color.main_color));
                viewParts.setVisibility(View.VISIBLE);
                recyclerviewPeijian.setVisibility(View.VISIBLE);

                break;
            case R.id.tv_jiance:
                timer();
                break;
            case R.id.tv_baoyang:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 4);
                RxActivityTool.skipActivity(this, SmallCDelicateCarWashActivity.class, bundle);
                break;
        }
    }

    private void timer() {
        ToastUtil.showProgressDialog(this);
        /** 倒计时3秒，一次1秒 */
        new CountDownTimer(3 * 1000, 3000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //倒计时的过程中回调该函数
                initOkGo();
            }

            @Override
            public void onFinish() {
                //倒计时结束时回调该函数
                ToastUtil.dismissProgressDialog();
            }
        }.start();
    }

    private void initOkGo() {
        OkGo.<String>get(Config.s + "llc/obdPeiZhiController/queryObdKeepHisList")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("qeqweqe", response.body());
                        Gson gson = new Gson();
                        QueryObdKeepHisListBean info = gson.fromJson(response.body(), QueryObdKeepHisListBean.class);
                        if (info.isIsSuccess()) {
                            mList1.clear();
                            mList2.clear();
                            mList1.addAll(info.getData().get_$1());
                            mList2.addAll(info.getData().get_$2());
                            mMaintainAdapter1.notifyDataSetChanged();
                            mMaintainAdapter2.notifyDataSetChanged();
                        } else {
                            RxToast.info(info.getOutputMessage());
                        }
                    }
                });
    }
}
